package com.gitee.kamismile.stone.basic.data.infrastructure.utils;

import com.gitee.kamismile.stone.commmon.util.ValueUtils;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.util.Arrays;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.util.CollectionUtils;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

public class CriteriaQueryBuilder {
    Criteria criteria=Criteria.empty();

    private CriteriaQueryBuilder() {
    }


    public CriteriaQueryBuilder eqIgnoreEmpty (String column, Object value ){
        if(Objects.isNull(value)){
            return this;
        }
        if(value instanceof String){
            if(StringUtils.isAllEmpty(ValueUtils.isStringNull(value,""))){
                return this;
            }
        }

        this.criteria=criteria.and(column).is(value);
        return this;
    }

    public CriteriaQueryBuilder inIgnoreEmpty (String column, Object[] value ){
        if(Arrays.isNullOrEmpty(value)){
            return this;
        }
        this.criteria=criteria.and(column).in(value);
        return this;
    }

    public CriteriaQueryBuilder inIgnoreEmpty (String column, Collection value ){
        if(CollectionUtils.isEmpty(value)){
            return this;
        }
        this.criteria=criteria.and(column).in(value);
        return this;
    }
    public CriteriaQueryBuilder likeIgnoreEmpty (String column, Object value ){
        if(Objects.isNull(value)){
            return this;
        }
        if(value instanceof String){
            if(StringUtils.isAllEmpty(ValueUtils.isStringNull(value,""))){
                return this;
            }
        }

        this.criteria=criteria.and(column).like("%"+value+"%");
        return this;
    }

    public Criteria  create(){
        return criteria;
    }

    public static CriteriaQueryBuilder builder() {
        return new CriteriaQueryBuilder();
    }
}
